<?php
class SectionHelper
{

	const SECTION_TYPE_TEXT = 0;
	const SECTION_TYPE_RIGHT_TEXT = 25; //右侧文本
	const SECTION_TYPE_TITLE = 1;
	const SECTION_TYPE_IMAGE = 3;
	const SECTION_TYPE_NOTE = 10;
	const SECTION_TYPE_PHP = 4;
	const SECTION_TYPE_SHELL = 5;
	const SECTION_TYPE_ORDERLIST = 6; //有序列表
	const SECTION_TYPE_DISORDERLIST = 7; //有序列表
	const SECTION_TYPE_SQL = 8; //SQL

	const SECTION_TYPE_CSS = 9; //SQL
	const SECTION_TYPE_RUBY = 11; //SQL
	const SECTION_TYPE_WARING = 2; //SQL
	const SECTION_TYPE_HTML = 20; //HTML
	const SECTION_TYPE_TABLE = 21; //TABLE
	const SECTION_TYPE_SUB_TITLE = 22; //sub Title
	const SECTION_TYPE_JS = 23; //JS
	const SECTION_TYPE_QUESTION_TITLE = 24; //问题 Title
	const SECTION_TYPE_TABLENO = 26; //左右无边表格
	
	public static function getSectionTypeList() {
		return array(
				self::SECTION_TYPE_TEXT => '文本',
				self::SECTION_TYPE_TITLE => '标题',
				self::SECTION_TYPE_SUB_TITLE => '子标题',
				self::SECTION_TYPE_QUESTION_TITLE=> '三级标题',
				self::SECTION_TYPE_IMAGE=> '图片',
				self::SECTION_TYPE_NOTE=>'提示',
				self::SECTION_TYPE_WARING=>'警告',
				self::SECTION_TYPE_PHP=>'PHP代码',
				self::SECTION_TYPE_RUBY=>'RUBY代码',
				self::SECTION_TYPE_SHELL=>'SHELL代码',
				self::SECTION_TYPE_SQL=>'SQL代码',
				self::SECTION_TYPE_CSS=>'CSS代码',
				self::SECTION_TYPE_ORDERLIST=>'有序列表',
				self::SECTION_TYPE_DISORDERLIST=>'无序列表',
				self::SECTION_TYPE_HTML=>'HTML代码',
				self::SECTION_TYPE_TABLE=>'表格',
				self::SECTION_TYPE_TABLENO=> '表格左右无边框',
				self::SECTION_TYPE_JS=>'JS代码',
				self::SECTION_TYPE_RIGHT_TEXT => '右侧文本',
		);
	}
	
	public static function getSectionType($type) {
		$typelist = self::getSectionTypeList();
		return $typelist[$type];
	}
	
	
	public static function replaceStaticUrl($str) {
		return str_replace('{STATIC_URL}', STATIC_URL, $str);
	}
	
	/**
	 * 格式化内容
	 * @param array $section
	 */
	public static function formatSection($section, $field='sectionText')
	{
		if ($section['isPost']==0) $_id = 'cn'.$section['sectionId'];
		else $_id = '';
		$_leftX = 30;
		$_paddingLeft = $_marginLeft = '';
		if ($section['leftX']!=0)  {
			$_paddingLeft='padding-left:'.($section['leftX']*$_leftX).'px;';
			$_marginLeft='margin-left:'.($section['leftX']*$_leftX).'px;';
		}
		$_sectionText = $section[$field];;
		$_sectionText = self::replaceStaticUrl($_sectionText);
		switch($section['sectionType']) {
			case self::SECTION_TYPE_TITLE:	//title
				$_sectionText = CHtml::tag('h3',array('class'=>'cn', 'id'=> $_id) , $_sectionText)."\n";
				break;
			case self::SECTION_TYPE_SUB_TITLE:	//sub title
				$_sectionText = CHtml::tag('h4',array('class'=>'cn', 'id'=> $_id) , trim($_sectionText))."\n";
				break;
			case self::SECTION_TYPE_QUESTION_TITLE:	//sub title
				$_sectionText = CHtml::tag('h5',array('class'=>'cn', 'id'=> $_id) , trim($_sectionText))."\n";
				break;
			case self::SECTION_TYPE_IMAGE:
				$_image = CHtml::image(STATIC_URL.'/'.$_sectionText, $_sectionText)."\n";
				$_sectionText = CHtml::tag('div',array('class'=>'figure', 'id'=> $_id) ,$_image)."\n";
				break;
			case self::SECTION_TYPE_WARING:
				$_notetitle = CHtml::tag('p',array('class'=>'first admonition-title') ,'警告')."\n";
				$_note = CHtml::tag('p',array('class'=>'last', 'style'=>$_paddingLeft) , $_sectionText)."\n";
				$_sectionText = CHtml::tag('div',array('class'=>'note cn', 'style'=>$_marginLeft, 'id'=> $_id) , $_note)."\n";
				break;
			case self::SECTION_TYPE_NOTE:
				$_notetitle = CHtml::tag('p',array('class'=>'first admonition-title') ,'提示')."\n";
				$_note = CHtml::tag('p',array('class'=>'last', 'style'=>$_paddingLeft) , $_sectionText)."\n";
				$_sectionText = CHtml::tag('div',array('class'=>'waring cn admonition-philosophy',  'style'=>$_marginLeft, 'id'=> $_id) , $_note)."\n";
				break;
			case self::SECTION_TYPE_JS:
				$h = new CTextHighlighter();
				$h->language='JAVASCRIPT';
				$html = $h->highlight($_sectionText);
				$_paddingtop = "";
				if (Yii::app()->params['codeTypeDesc']) $_paddingtop="padding-top:5px;";
				$_sectionText  = '<div class="cn" id='. $_id .'  style="'. $_paddingtop . $_paddingLeft .'" >';
				if (Yii::app()->params['codeTypeDesc']) $_sectionText .= '<div style="margin:-10px;padding:0">JS代码：</div>';
				$_sectionText .= $html;
				$_sectionText .='</div>';
				break;
			case self::SECTION_TYPE_PHP:
				$h = new CTextHighlighter();
				$h->language='php';
				$html = $h->highlight($_sectionText);
				$_paddingtop = "";
				if (Yii::app()->params['codeTypeDesc']) $_paddingtop="padding-top:5px;";
				$_sectionText  = '<div class="cn" id='. $_id .'  style="'. $_paddingtop . $_paddingLeft .'" >';
				if (Yii::app()->params['codeTypeDesc']) $_sectionText .= '<div style="margin:-10px;padding:0">PHP代码：</div>';
				$_sectionText .= $html;
				$_sectionText .='</div>';
				break;
			case self::SECTION_TYPE_SHELL:
				$h = new CTextHighlighter();
				$h->language='shell';
				$html = $h->highlight($_sectionText);
				$_paddingtop = "";
				if (Yii::app()->params['codeTypeDesc']) $_paddingtop="padding-top:5px;";
				$_sectionText  = '<div class="cn" id='. $_id .'  style="'. $_paddingtop . $_paddingLeft .'" >';
				if (Yii::app()->params['codeTypeDesc']) $_sectionText .= '<div style="margin:-10px;padding:0">SHELL代码或屏幕回显：</div>';
				$_sectionText .= $html;
				$_sectionText .='</div>';
				break;
			case self::SECTION_TYPE_HTML:
				$h = new CTextHighlighter();
				$h->language='html';
				$html = $h->highlight($_sectionText);
				$_paddingtop = "";
				if (Yii::app()->params['codeTypeDesc']) $_paddingtop="padding-top:5px;";
				$_sectionText  = '<div class="cn" id='. $_id .'  style="'. $_paddingtop . $_paddingLeft .'" >';
				if (Yii::app()->params['codeTypeDesc']) $_sectionText .= '<div style="margin:-10px;padding:0">HTML代码：</div>';
				$_sectionText .= $html;
				$_sectionText .='</div>';
				break;
			case self::SECTION_TYPE_SQL:
				$h = new CTextHighlighter();
				$h->language='sql';
				$html = $h->highlight($_sectionText);
				$_paddingtop = "";
				if (Yii::app()->params['codeTypeDesc']) $_paddingtop="padding-top:5px;";
				$_sectionText  = '<div class="cn" id='. $_id .'  style="'. $_paddingtop . $_paddingLeft .'" >';
				if (Yii::app()->params['codeTypeDesc']) $_sectionText .= '<div style="margin:-10px;padding:0">SQL代码：</div>';
				$_sectionText .= $html;
				$_sectionText .= '</div>';
				break;
			case self::SECTION_TYPE_CSS:
				$h = new CTextHighlighter();
				$h->language='css';
				$html = $h->highlight($_sectionText);
				$_paddingtop = "";
				if (Yii::app()->params['codeTypeDesc']) $_paddingtop="padding-top:5px;";
				$_sectionText  = '<div class="cn" id='. $_id .'  style="'. $_paddingtop . $_paddingLeft .'" >';
				if (Yii::app()->params['codeTypeDesc']) $_sectionText .= '<div style="margin:-10px;padding:0">CSS代码：</div>';
				$_sectionText .= $html;
				$_sectionText .= '</div>';
				break;
			case self::SECTION_TYPE_ORDERLIST:
				$_sectionText = CHtml::tag('ol',array('class'=>'cn arabic simple', 'id'=> $_id, 'start'=>$section['olStart']) , $_sectionText)."\n";
				break;
			case self::SECTION_TYPE_DISORDERLIST:
				$_sectionText = CHtml::tag('ul',array('class'=>'cn arabic simple', 'style'=>$_paddingLeft, 'id'=> $_id) , $_sectionText)."\n";
				break;
			case self::SECTION_TYPE_TABLE:
				$_sectionText = self::createTableByString($_sectionText, $_marginLeft, $section['olStart']);
				break;
			case self::SECTION_TYPE_TABLENO:
				$_sectionText = self::createTableByString($_sectionText,$_marginLeft, $section['olStart'], false);
				break;
			case self::SECTION_TYPE_RIGHT_TEXT:
				$_sectionText = CHtml::tag('p',array('class'=>'cn', 'style'=>'text-align:right;', 'id'=> $_id) ,$_sectionText)."\n";
				break;
			case self::SECTION_TYPE_TEXT:
			default :
				$_sectionText = CHtml::tag('p',array('class'=>'cn', 'style'=>$_paddingLeft, 'id'=> $_id) ,$_sectionText)."\n";
		}
		return $_sectionText;
	}
	
	public static function createTableByString($str, $_marginLeft, $_width, $isBorder=true)
	{
		$borderleft = $borderright = '';
		if ($isBorder==false) {
			$borderleft = 'border-left:none;';
			$borderright = 'border-right:none;';
		}
		if ($_marginLeft=='') $_marginLeft = 'margin: 0 auto;'; //居中
		if ($_width==1) $width='width:95%;'; 
		else $width = 'width:'.$_width.'%;';
		
		$row = explode("{Y!!}", $str);
		if (count($row)<2) return $str;
		$table = '<table style="'.$width.';border-collapse:collapse;'. $_marginLeft .'">';
		//make col group
		$cg = explode("{YII}", $row[0]);
		$cgs = '<colgroup>';
		for($j = 0; $j<count($cg); $j++) {
			$cgs .= '<col style="width:'. trim($cg[$j]) .';">';
		}
		$cgs .= '</colgroup>';

		$th = array();
		if ( trim($row[1])!='{--}' ) {
			//make th;
			$th = explode("{YII}", $row[1]);
			$ths = '<tr>';
			for($j = 0; $j<count($th); $j++) {
				if ($j==0) 
					$border = $borderleft;
				else if ($j==(count($th)-1)) 
					$border = $borderright;
				else $border = 0;
				$ths .= '<th style="border:1px solid #000;'.$border.'padding:2px;"><b>'. trim($th[$j]) .'</b></th>';
			}
			$ths .= '</tr>';
		}
		else $ths = '';
		
		//make td
		$tds = '';
		for($i=2; $i<count($row); $i++) {
			if (trim($row[$i])=='') continue;
			$td = explode('{YII}', trim($row[$i]));
			
			$tds .= '<tr>';
			for($j=0; $j< count($td); $j++) {
				if ($j==0)
					$border = $borderleft;
				else if ($j==(count($th)-1))
					$border = $borderright;
				else $border = 0;
				$tds .= '<td style="border:1px solid #000;'.$border.'padding:2px;">'. trim($td[$j]) .'</td>';
			} 
			$tds .= '</tr>';
		}
		$table .= $cgs.$ths.$tds;
		$table .= '</table>';
		
		//echo $table;exit;
		return $table;
	}
}
